<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if='filterParams && !filterLoading'>
    <!-- 品牌筛选条件 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click='filterParams.brands.selectedBrand=item.id' :class='{active: filterParams.brands.selectedBrand===item.id}' href="javascript:;" v-for="item in filterParams.brands.list" :key="item.id">{{item.name}}</a>
      </div>
    </div>
    <!-- 其他筛选条件 -->
    <div class="item" v-for="item in filterParams.saleProperties" :key="item.id">
      <div class="head">{{item.name}}：</div>
      <div class="body">
        <a @click='item.selectedProp=tag.id' :class='{active: item.selectedProp===tag.id}' href="javascript:;" v-for="tag in item.properties" :key="tag.id">{{tag.name}}</a>
      </div>
    </div>
  </div>
  <!-- 如果没有数据就显示骨架屏 -->
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category.js'

export default {
  name: 'SubFilter',
  emits: ['filter-change'],
  setup (props, { emit }) {
    // 筛选条件数据
    const filterParams = ref(null)
    const route = useRoute()

    // 筛选条件数据加载的状态位
    const filterLoading = ref(false)

    // 侦听路由参数的变化，重新调用接口获取最新筛选数据
    watch(() => route.params.id, (newVal) => {
      // 必须验证二级路由的切换
      if (route.fullPath === '/category/sub/' + newVal) {
        // 此时接口数据正在加载
        filterLoading.value = true
        findSubCategoryFilter(newVal).then(ret => {
          if (ret.result.brands) {
            // // 添加选中的属性，用于记录选中的条件值
            // ret.result.brands.selectedBrand = null

            ret.result.brands.unshift({
              id: null,
              name: '全部'
            })
          }
          if (ret.result.saleProperties) {
            ret.result.saleProperties.forEach(item => {
              // 添加选中的属性，用于记录选中的条件值
              item.selectedProp = null
              item.properties.unshift({
                id: null,
                name: '全部'
              })
            })
          }
          filterParams.value = {
            // brands: ret.result.brands,
            brands: {
              list: ret.result.brands,
              selectedBrand: null
            },
            saleProperties: ret.result.saleProperties
          }
          // 数据加载完成
          filterLoading.value = false
        })
      }
    }, {
      immediate: true
    })

    // 侦听数据的变化
    // 自己添加到数组中的自定义属性值变化是侦听器无法监听到，但是对象中可以
    watch(() => filterParams, (newVal) => {
      // 获取规格参数
      const attrs = []
      newVal.value.saleProperties && newVal.value.saleProperties.forEach(item => {
        const param = {
          // 左侧名称
          groupName: item.name,
          // 右侧选项名称
          propertyName: item.selectedProp ? item.properties.find(tag => tag.id === item.selectedProp).name : null
        }
        attrs.push(param)
      })
      emit('filter-change', {
        brandId: filterParams.value.brands.selectedBrand,
        arttrs: attrs
      })
    }, {
      deep: true
    })

    return { filterParams, filterLoading }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .xtx-skeleton {
    padding: 10px 0;
  }
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
